package com.haotian.dataconvert.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.haotian.dataconvert.Dto;

public class HashDto<K, V> extends HashMap<K, V> implements Dto<K, V> {

	private static final long serialVersionUID = 4544981999408539563L;

	public HashDto() {
		super();
	}

	public HashDto(Map<? extends K, ? extends V> m) {
		super(m);
	}

	/**
	 * type inference 类型推导方式创建HashDto
	 * 
	 * @return
	 */
	public static <K, V> HashDto<K, V> newInstance() {
		return new HashDto<K, V>();
	}

	@Override
	public String getAsString(K key) {
		V v = get(key);
		if (v == null)
			return null;
		else if (v instanceof String)
			return (String) v;
		else
			return String.valueOf(v);
	}

	@Override
	public Integer getAsInteger(K key) {
		V v = get(key);
		if (v == null)
			return null;
		else if (v instanceof String)
			return Integer.parseInt((String) v);
		else if (v instanceof Integer)
			return (Integer) v;
		else if (v instanceof Double)
			return ((Double) v).intValue();
		else if (v instanceof Boolean)
			return ((Boolean) v) ? 1 : 0;
		return null;
	}

	@Override
	public Double getAsDouble(K key) {
		V v = get(key);
		if (v == null)
			return null;
		if (v instanceof Double)
			return (Double) v;
		else if (v instanceof String)
			return Double.parseDouble((String) v);
		else if (v instanceof Integer)
			return Double.parseDouble(Integer.toString((Integer) v));
		return null;
	}

	@Override
	public Boolean getAsBoolean(K key) {
		V v = get(key);
		if (v == null)
			return null;
		else if (v instanceof String)
			return Boolean.parseBoolean((String) v);
		else if (v instanceof Integer)
			return ((Integer) v) == 0 ? false : true;
		else
			return (Boolean) v;
	}

	@Override
	public Date getAsDate(K key) {
		V v = get(key);
		if (v == null) {
			return null;
		} else if (v instanceof String) {
			long time = Long.parseLong((String) v);
			return new Date(time);
		} else if (v instanceof Long) {
			return new Date((Long) v);
		} else if (v instanceof Integer) {
			return new Date((Integer) v);
		} else {
			return (Date) v;
		}

	}

	@Override
	public String getAsDate(K key, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(getAsDate(key));
	}

	@Override
	public String getAsJsonString(K key) {
		V v = get(key);
		if (v == null) {
			return null;
		}
		return JSON.toJSONString(v);
	}

	@Override
	public String getAsJsonString(K key, String format) {
		V v = get(key);
		if (v == null) {
			return null;
		}
		SerializeConfig config = new SerializeConfig();
		config.put(java.util.Date.class, new SimpleDateFormatSerializer(format));
		return JSON.toJSONString(v, config, SerializerFeature.PrettyFormat);
	}

	@Override
	public List<?> getAsList(K key) {
		V v = get(key);
		if (v == null) {
			return null;
		} else if (v instanceof List) {
			return (List<?>) v;
		}
		return null;
	}

	@Override
	public String toJSONString() {
		return JSON.toJSONString(this);
	}

}
